package com.itheima.service;

import com.itheima.autoconfig.template.OssTemplate;
import com.itheima.vo.MinioVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 芝士雪豹
 * @create 2022/10/11/16:15
 */
@Service
public class MinioService {

    @Autowired
    private OssTemplate ossTemplate;

    //文件上传
    public MinioVo upload(MultipartFile file) {
        MinioVo vo = new MinioVo();
        String url = null;
        try {
            url = ossTemplate.upload(file.getOriginalFilename(), file.getInputStream());
        } catch (IOException e) {
            vo.setCode(404);
            vo.setMessage("文件上传失败");
            return vo;
        }
        Map map = new HashMap();
        map.put("url", url);
        vo.setCode(200);
        vo.setDate(map);
        vo.setMessage("文件上传成功");
        return vo;
    }
}
